<?php
/**
 * Created by IntelliJ IDEA.
 * User: Jason
 * Date: 2018-12-03
 * Time: 14:27
 */

namespace app\member\controller;


use cmf\controller\AdminBaseController;
use think\Db;
use think\Exception;

/**
 * 套餐管理控制器
 * Class AdminPackController
 * @package app\member\controller
 */
class AdminPackController extends AdminBaseController
{

    public function index()
    {
        $pack_name = request()->param('pack_name');
        $where =['status'=>1000,'mark'=>2000];

        if($pack_name){
            $this->assign('pack_name',$pack_name);
            $where['pack_name']=['like','%'.$pack_name.'%'];
        }

        $pack = Db::name('member_pack')->where($where)->paginate(20);
        $page= $pack->render();

        $this->assign('pack',$pack);
        $this->assign('page',$page);
        return $this->fetch();
    }

    public function add()
    {
        $where = ['status'=>1000,'mark'=>2000];
        $item = Db::name('member_item')->where($where)->field('id,item_name')->select();
        $this->assign('item',$item);
        return $this->fetch();
    }

    public function addPost()
    {
        $now = time();
        $params = $this->request->param();
        Db::startTrans();
        try{
            $pack_data = [
                'pack_name'=>$params['pack_name'],
                'price'=>$params['price'],
                'mark'=>2000,
                'status'=>1000,
                'add_time'=>$now,
                'update_time'=>$now
            ];
            Db::name('member_pack')->insert( $pack_data );
            $pack_id = Db::name('member_pack')->getLastInsID();
            $data = [];
            foreach($params['ids'] as $k=>$v){
                if($v)
                    $data[] = [ 'pack_id'=>$pack_id,'item_id'=>$k,'number'=>$v,'status'=>1000,'add_time'=>$now,'update_time'=>$now ];
            }
            if($data)
                Db::name('member_pack_item')->insertAll($data);
            Db::commit();
            return $this->success("添加成功！",url('AdminPack/index'));
        }catch(Exception $e){
            return $this->error('添加失败');
        }

    }

    public function edit()
    {
        $id = $this->request->param('id');
        $pack = Db::name('member_pack')->alias('a')
            ->join('member_pack_item b','a.id=b.pack_id')
            ->join('member_item c','c.id=b.item_id')
            ->field('a.pack_name,a.price,b.*,c.item_name')
            ->where('a.id',$id)
            ->select();
        $this->assign('pack',$pack);
        return $this->fetch();
    }

    public function editPost()
    {
        $now = time();
        $params = $this->request->param();
        Db::name('member_pack')->where('id',$params['id'])->update( ['price'=>$params['price'],'pack_name'=>$params['pack_name']] );
        foreach($params['ids'] as $k => $v){
            $res = Db::name('member_pack_item')->update(['id'=>$k,'number'=>$v,'update_time'=>$now]);
        }
        if($res)
            return $this->success("更新成功！",url('AdminPack/edit',array('id'=>$params['id'])));
        else
            return $this->error('更新失败');
    }

    public function info()
    {
        $pack_id = $this->request->param('id');
        $pack = Db::name('member_pack')->where('id',$pack_id)->field('id,pack_name')->find();
        $this->assign('pack',$pack);
        $pack_item =Db::name('member_pack_item')->alias('a')
            ->join('member_item b', 'a.item_id=b.id')
            ->where('a.pack_id',$pack_id)
            ->field('a.number,a.id pack_item_id,b.*')
            ->select();
        $this->assign('pack_item',$pack_item);
        return $this->fetch();
    }

    public function infoPost()
    {
        $param = $this->request->param();
        $res = [];
        foreach($param['pack_item_id'] as $k=>$v ){
            $res[] = Db::name('member_pack_item')->update( ['id'=>$k,'number'=>$v]);
        }
        if($res)
            return $this->success("更新成功！");
        else
            return $this->error('更新失败');
    }

    public function delete()
    {
        $params = $this->request->param();
        $where['id'] = ['in',$params['ids'] ];
        $res = Db::name('member_pack')->where($where)->update([ 'status'=>1001,'delete_time'=>time() ]);
        if($res)
            return $this->success("删除成功！");
        else
            return $this->error('删除失败');
    }

}